package database;

//package au.com.bytecode.opencsv;

/**
Copyright 2005 Bytecode Pty Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

/**
 * A very simple CSV reader released under a commercial-friendly license.
 * 
 * @author Glen Smith
 * @author Tristan Daniel Boucher (Formatting)
 * @version Commit #24
 * 
 */
public class CSVReader implements Closeable 
{
  /**
   * The default line to start reading.
   */
  public static final int DEFAULT_SKIP_LINES = 0;

  /**
   * The buffered reader object.
   */
  private BufferedReader my_br;

  /**
   * Does the file have more?
   */
  private boolean my_has_next = true;

  /**
   * The CSVParser object.
   */
  private CSVParser my_parser;

  /**
   * How many lines to skip?
   */
  private int my_skip_lines;

  /**
   * The amount of lines skipped. Some may be skipped due to empty whitespace.
   */
  private boolean my_lines_skipped;

  /**
   * Constructs CSVReader using a comma for the separator.
   * 
   * @param the_reader
   *            the reader to an underlying CSV source.
   */
  public CSVReader(final Reader the_reader) 
  {
    this(the_reader, CSVParser.DEFAULT_SEPARATOR, 
         CSVParser.DEFAULT_QUOTE_CHARACTER, CSVParser.DEFAULT_ESCAPE_CHARACTER);
  }

  /**
   * Constructs CSVReader with supplied separator.
   * 
   * @param the_reader
   *            the reader to an underlying CSV source.
   * @param the_separator
   *            the delimiter to use for separating entries.
   */
  public CSVReader(final Reader the_reader, final char the_separator) 
  {
    this(the_reader, the_separator, 
         CSVParser.DEFAULT_QUOTE_CHARACTER, CSVParser.DEFAULT_ESCAPE_CHARACTER);
  }

  /**
   * Constructs CSVReader with supplied separator and quote char.
   * 
   * @param the_reader
   *            the reader to an underlying CSV source.
   * @param the_separator
   *            the delimiter to use for separating entries
   * @param the_quotechar
   *            the character to use for quoted elements
   */
  public CSVReader(final Reader the_reader, final char the_separator, 
                   final char the_quotechar) 
  {
    this(the_reader, the_separator, 
         the_quotechar, CSVParser.DEFAULT_ESCAPE_CHARACTER, 
         DEFAULT_SKIP_LINES, CSVParser.DEFAULT_STRICT_QUOTES);
  }

  /**
   * Constructs CSVReader with supplied separator, quote char and quote handling
   * behavior.
   *
   * @param the_reader
   *            the reader to an underlying CSV source.
   * @param the_separator
   *            the delimiter to use for separating entries
   * @param the_quotechar
   *            the character to use for quoted elements
   * @param the_strict_quotes
   *            sets if characters outside the quotes are ignored
   */
  public CSVReader(final Reader the_reader, final char the_separator, 
                   final char the_quotechar, final boolean the_strict_quotes) 
  {
    this(the_reader, the_separator, 
         the_quotechar, CSVParser.DEFAULT_ESCAPE_CHARACTER, 
         DEFAULT_SKIP_LINES, the_strict_quotes);
  }

  /**
   * Constructs CSVReader with supplied separator and quote char.
   *
   * @param the_reader
   *            the reader to an underlying CSV source.
   * @param the_separator
   *            the delimiter to use for separating entries
   * @param the_quotechar
   *            the character to use for quoted elements
   * @param the_escape
   *            the character to use for escaping a separator or quote
   */

  public CSVReader(final Reader the_reader, final char the_separator,
                   final char the_quotechar, final char the_escape) 
  {
    this(the_reader, the_separator, 
         the_quotechar, the_escape, 
         DEFAULT_SKIP_LINES, CSVParser.DEFAULT_STRICT_QUOTES);
  }

  /**
   * Constructs CSVReader with supplied separator and quote char.
   * 
   * @param the_reader
   *            the reader to an underlying CSV source.
   * @param the_separator
   *            the delimiter to use for separating entries
   * @param the_quotechar
   *            the character to use for quoted elements
   * @param the_line
   *            the line number to skip for start reading 
   */
  public CSVReader(final Reader the_reader, final char the_separator, 
                   final char the_quotechar, final int the_line) 
  {
    this(the_reader, the_separator, 
         the_quotechar, CSVParser.DEFAULT_ESCAPE_CHARACTER, 
         the_line, CSVParser.DEFAULT_STRICT_QUOTES);
  }

  /**
   * Constructs CSVReader with supplied separator and quote char.
   *
   * @param the_reader
   *            the reader to an underlying CSV source.
   * @param the_separator
   *            the delimiter to use for separating entries
   * @param the_quotechar
   *            the character to use for quoted elements
   * @param the_escape
   *            the character to use for escaping a separator or quote
   * @param the_line
   *            the line number to skip for start reading
   */
  public CSVReader(final Reader the_reader, final char the_separator, 
                   final char the_quotechar, final char the_escape, 
                   final int the_line) 
  {
    this(the_reader, the_separator, 
         the_quotechar, the_escape, 
         the_line, CSVParser.DEFAULT_STRICT_QUOTES);
  }

  /**
   * Constructs CSVReader with supplied separator and quote char.
   * 
   * @param the_reader
   *            the reader to an underlying CSV source.
   * @param the_separator
   *            the delimiter to use for separating entries
   * @param the_quotechar
   *            the character to use for quoted elements
   * @param the_escape
   *            the character to use for escaping a separator or quote
   * @param the_line
   *            the line number to skip for start reading
   * @param the_strict_quotes
   *            sets if characters outside the quotes are ignored
   */
  public CSVReader(final Reader the_reader, final char the_separator, 
                   final char the_quotechar, final char the_escape, 
                   final int the_line, final boolean the_strict_quotes) 
  {
    this(the_reader, the_separator, 
         the_quotechar, the_escape, 
         the_line, the_strict_quotes, 
         CSVParser.DEFAULT_IGNORE_LEADING_WHITESPACE);
  }

  /**
   * Constructs CSVReader with supplied separator and quote char.
   * 
   * @param the_reader
   *            the reader to an underlying CSV source.
   * @param the_separator
   *            the delimiter to use for separating entries
   * @param the_quotechar
   *            the character to use for quoted elements
   * @param the_escape
   *            the character to use for escaping a separator or quote
   * @param the_line
   *            the line number to skip for start reading
   * @param the_strict_quotes
   *            sets if characters outside the quotes are ignored
   * @param the_ignore_leading_white_space
   *            it true, parser should ignore white space before a quote in a field
   */
  public CSVReader(final Reader the_reader, final char the_separator, 
                   final char the_quotechar, final char the_escape, 
                   final int the_line, final boolean the_strict_quotes, 
                   final boolean the_ignore_leading_white_space) 
  {
    this.my_br = new BufferedReader(the_reader);
    this.my_parser = new CSVParser(the_separator, the_quotechar, 
                                   the_escape, the_strict_quotes, 
                                   the_ignore_leading_white_space);
    this.my_skip_lines = the_line;
  }

  /**
   * Reads the entire file into a List with each element being a String[] of
   * tokens.
   * 
   * @return a List of String[], with each String[] representing a line of the
   *         file.
   * 
   * @throws IOException
   *             if bad things happen during the read
   */
  public List<String[]> readAll() throws IOException 
  {

    final List<String[]> all_elements = new ArrayList<String[]>();
    while (my_has_next) 
    {
      final String[] next_line_as_token = readNext();
      if (next_line_as_token != null)
      {
        all_elements.add(next_line_as_token);
      }
    }
    return all_elements;

  }

  /**
   * Reads the next line from the buffer and converts to a string array.
   * 
   * @return a string array with each comma-separated element as a separate
   *         entry.
   * 
   * @throws IOException
   *             if bad things happen during the read
   */
  public String[] readNext() throws IOException 
  {
    String[] result = null;
    do 
    {
      final String next_line = getNextLine();
      if (!my_has_next) 
      {
        return result; // should throw if still pending?
      }
      final String[] r = my_parser.parseLineMulti(next_line);
      if (r.length > 0) 
      {
        if (result == null) 
        {
          result = r;
        } 
        else 
        {
          final String[] t = new String[result.length + r.length];
          System.arraycopy(result, 0, t, 0, result.length);
          System.arraycopy(r, 0, t, result.length, r.length);
          result = t;
        }
      }
    } while (my_parser.isPending());
    return result;
  }

  /**
   * Reads the next line from the file.
   * 
   * @return the next line from the file without trailing newline
   * @throws IOException
   *             if bad things happen during the read
   */
  private String getNextLine() throws IOException 
  {
    if (!this.my_lines_skipped) 
    {
      for (int i = 0; i < my_skip_lines; i++) 
      {
        my_br.readLine();
      }
      this.my_lines_skipped = true;
    }
    final String next_line = my_br.readLine();
    if (next_line == null) 
    {
      my_has_next = false;
    }
    return my_has_next ? next_line : null;
  }

  /**
   * Closes the underlying reader.
   * 
   * @throws IOException if the close fails
   */
  public void close() throws IOException
  {
    my_br.close();
  }

}
//End of Class CSVReader.